﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBAccessLib;
using System.Configuration;

namespace LineWatch_Web
{
    public enum DataSourceType
    {
        Start,
        View,
    }
    public partial class review : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] == null || Session["UserName"].ToString().Equals(""))
                {
                    Response.Redirect("ProductManagement.aspx");
                }
                Calendar1.Text = DateTime.Now.ToString("yyyy-MM-dd");
                Calendar2.Text = DateTime.Now.ToString("yyyy-MM-dd");

                BindLine();

                labPosition.Visible = false;
                txtPosition.Visible = false;
                btnCalculate.Visible = false;
            }
        }

        private void BindLine()
        {
            DataTable dt = Product.GetAllLines();
            ddlLine.Items.Clear();
            ddlLine.Items.Add("全部");
            foreach (DataRow row in dt.Rows)
            {
                ListItem li = new ListItem(row["Line_Name"].ToString().Trim(), row["Line_Code"].ToString().Trim());
                ddlLine.Items.Add(li);
            }
        }

        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlView.SelectedItem.Value.Equals("position"))
            {
                labPosition.Visible = true;
                txtPosition.Visible = true;
                btnCalculate.Visible = true;
            }
            else
            {
                labPosition.Visible = false;
                txtPosition.Visible = false;
                btnCalculate.Visible = false;
            }
        }

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            gvStat.PageIndex = 0;
            BindStat();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ViewState["CurrentDataSourceType"] != null)
            {
                DataTable dt = null;
                string fileName = string.Empty;
                DataSourceType type = (DataSourceType)ViewState["CurrentDataSourceType"];
                switch (type)
                {
                    case DataSourceType.Start:
                        dt = GetStartDataSource();
                        fileName = String.Format("S{0}.xls", DateTime.Now.ToString("yyyyMMddhhmmss"));
                        break;
                    case DataSourceType.View:
                        dt = GetViewDataSource();
                        fileName = String.Format("H{0}.xls", DateTime.Now.ToString("yyyyMMddhhmmss"));
                        break;
                    default:
                        break;
                }
                CreateExcel(dt, fileName);
            }
        }

        public void CreateExcel(DataTable dt, string FileName)
        {
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentType = "application/ms-excel";
            resp.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationManager.AppSettings["CSVEncoding"]);
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            string colHeaders = "", ls_item = "";
            int i = 0;
            DataRow[] myRow = dt.Select("");
            for (i = 0; i < dt.Columns.Count - 1; i++)
            {
                colHeaders += ColumnCaptionMapping(dt.Columns[i].Caption.ToString().Trim()) + "\t";
            }
            colHeaders += ColumnCaptionMapping(dt.Columns[i].Caption.ToString().Trim()) + "\n";
            resp.Write(colHeaders);
            foreach (DataRow row in myRow)
            {
                for (i = 0; i < dt.Columns.Count - 1; i++)
                {
                    ls_item += row[i].ToString().Trim() + "\t";
                }
                ls_item += row[i].ToString().Trim() + "\n";
                resp.Write(ls_item);
                ls_item = "";
            }
            resp.End();
        }

        protected void gvStat_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvStat.PageIndex = e.NewPageIndex;
            BindStat();
        }

        private void BindStat()
        {
            DataTable dt = GetStartDataSource();
            ViewState["CurrentDataSourceType"] = DataSourceType.Start;

            foreach (DataColumn dc in dt.Columns) dc.ColumnName = ColumnCaptionMapping(dc.ColumnName.ToUpper());

            gvStat.DataSource = dt;
            gvStat.DataBind();
            gvView.Visible = false;
            gvStat.Visible = true;
        }

        private DataTable GetStartDataSource()
        {
            string from = Calendar1.Text;
            string to = Calendar2.Text;
            string line = null;
            string shift = null;
            string position = null;
            if (ddlLine.SelectedIndex != 0) line = ddlLine.SelectedItem.Value;
            if (ddlShift.SelectedItem.Value != "0") shift = ddlShift.SelectedItem.Value;
            if (!string.IsNullOrWhiteSpace(txtPosition.Text)) position = txtPosition.Text;
            DataTable dt = Product.GetStartSummary(from, to, line, shift, position);
            return dt;
        }

        private string ColumnCaptionMapping(string colCaption)
        {
            if (colCaption.Equals("LINE_CODE"))
                return "流水线号";
            else if (colCaption.Equals("LOGDATE"))
                return "生产日期";
            else if (colCaption.Equals("SHIFT"))
                return "班次";
            else if (colCaption.Equals("PLANNUM"))
                return "今日计划";
            else if (colCaption.Equals("NOWPLAN"))
                return "现时计划";
            else if (colCaption.Equals("PRODUCTNUM"))
                return "现时完成";
            else if (colCaption.Equals("STOPTIME"))
                return "停止时间";
            else if (colCaption.Equals("LOGTIME"))
                return "记录时间";
            else if (colCaption.Equals("SPOSITIONID"))
                return "系统工位号";
            else if (colCaption.Equals("FPOSITIONID"))
                return "实际工位号";
            else if (colCaption.Equals("POSITION"))
                return "工位名称";
            else if (colCaption.Equals("TYPE"))
                return "类型";
            else if (colCaption.Equals("HELPTIME"))
                return "求助时间";
            else if (colCaption.Equals("ALARMTIME"))
                return "紧停时间";
            else
                return "";
        }

        protected void gvStat_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#80DDFF',this.style.fontWeight='';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            gvView.PageIndex = 0;
            BindView();
        }

        private void BindView()
        {
            DataTable dt = GetViewDataSource();
            ViewState["CurrentDataSourceType"] = DataSourceType.View;

            foreach (DataColumn dc in dt.Columns) dc.ColumnName = ColumnCaptionMapping(dc.ColumnName.ToUpper());

            gvView.DataSource = dt;
            gvView.DataBind();
            gvStat.Visible = false;
            gvView.Visible = true;
        }

        private DataTable GetViewDataSource()
        {
            string from = Calendar1.Text;
            string to = Calendar2.Text;
            string line = null;
            string shift = null;
            string position = null;
            DataTable dt = null;
            if (ddlLine.SelectedIndex != 0) line = ddlLine.SelectedItem.Value;
            if (ddlShift.SelectedItem.Value != "0") shift = ddlShift.SelectedItem.Value;
            if (ddlView.SelectedItem.Value == "product") dt = Product.GetProduct(from, to, line, shift);
            else
            {
                if (!string.IsNullOrWhiteSpace(txtPosition.Text)) position = txtPosition.Text;
                dt = Product.GetStart(from, to, line, shift, position);
            }
            return dt;
        }

        protected void gvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvView.PageIndex = e.NewPageIndex;
            BindView();
        }

        protected void gvView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#80DDFF',this.style.fontWeight='';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            }
        }
    }
}